#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/9/26 10:32
# @Author  : qinmin
# @File    : views_base.py

from django.views.generic.base import View

import logging
# 生成一个以当前文件名为名字的logger实例
logger = logging.getLogger(__name__)
# 生成一个名为collect的logger实例
collect_logger = logging.getLogger("collect")

from .models import Goods
class GoodsListView2(View):
    def get(self,request):
        """
        通过django的view实现商品列表页
        :param request:
        :return:
        """
        json_list = []
        goods = Goods.objects.all()[:10]
        for good in goods:
            json_dict = {}
            json_dict["name"] = good.name
            json_dict["category"] = good.category.name
            json_dict["market_price"] = good.market_price
            # json_dict["add_time"] = good.add_time
            json_list.append(json_dict)
        # from django.forms.models import model_to_dict
        logger.info(json_list)
        logger.debug("debug logs is :" + str(json_list))
        from django.http import HttpResponse
        import json
        return HttpResponse(json.dumps(json_list),content_type="application/json")

class GoodsListView(View):
    def get(self,request):
        """
        通过django的view实现商品列表页
        :param request:
        :return:
        """
        json_list = []
        goods = Goods.objects.all()[:10]

        # from django.forms.models import model_to_dict
        # for good in goods:
        #     json_dict = model_to_dict(good)
        #     json_list.append(json_dict)
        # logger.info(json_list)
        # logger.debug("debug logs is :" + str(json_list))
        from django.http import HttpResponse,JsonResponse
        import json
        from django.core import serializers
        json_data = serializers.serialize('json',goods)
        json_data = json.loads(json_data)
        logger.info("json_data is --->" + str(json_data))
        # return HttpResponse(json.dumps(json_list),content_type="application/json")
        return JsonResponse(json_data,safe=False)
